import { Router } from "express";
import { AuthMiddleware } from "../../../core/middleware/auth.js";
import { AdminProductController } from "./controller.js";
import { errorHandler } from "./../../../core/middleware/errorHandler.js";
import {
  getProductListSchema,
  batchDeleteProductsSchema,
  createProductSchema,
  updateProductSchema,
  updateProductStatusSchema,
  createSpecTypeSchema, updateSpecTypeSchema,
} from "./schema.js";

import { API_URLS } from "../../../config/api-urls.js";

const router = Router();
const ADMIN_ROLES = ["admin", "superadmin"];

// 管理员权限检查中间件
const requireAdmin = AuthMiddleware.authorize(ADMIN_ROLES);

// 商品分类列表
router.get(
  API_URLS.GET_PRODUCT_CATEGORY_LIST,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  AdminProductController.getCategoryList
);

// 商品列表
router.get(
  API_URLS.GET_PRODUCT_LIST,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  AuthMiddleware.validate(getProductListSchema),
  AdminProductController.getProductsList
);

// 商品详情
router.get(
  API_URLS.GET_PRODUCT_DETAIL,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  AdminProductController.getProductInfo
);
// 商品详情获取SKU列表
router.get(
  API_URLS.GET_PRODUCT_SKU_LIST,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  AdminProductController.getProductSkuList
);

// 创建商品
router.post(
  API_URLS.PRODUCT_CREATE,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AuthMiddleware.validate(createProductSchema),
  AdminProductController.createProduct
);

// 更新商品
router.put(
  API_URLS.PUT_PRODUCT_UPDATE,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AuthMiddleware.validate(updateProductSchema),
  AdminProductController.updateProduct
);

// 批量删除商品
router.post(
  API_URLS.BATCH_DELETE_PRODUCTS,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AuthMiddleware.validate(batchDeleteProductsSchema),
  AdminProductController.batchDeleteProducts
);

// 删除商品
router.delete(
  API_URLS.DELETE_PRODUCT,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AdminProductController.deleteProduct
);

// 恢复商品
router.post(
  API_URLS.RESTORE_PRODUCT,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AdminProductController.restoreProduct
);

// 更新商品状态
router.put(
  API_URLS.UPDATE_PRODUCT_STATUS,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AuthMiddleware.validate(updateProductStatusSchema),
  AdminProductController.updateProductStatus
);

// 获取所有规格类型
router.get(
  API_URLS.GET_SPEC_TYPE_LIST,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AdminProductController.getSpecTypeList
);
// 创建规格类型
router.post(
  API_URLS.SPEC_TYPE_CREATE,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AuthMiddleware.validate(createSpecTypeSchema),
  AdminProductController.createSpecType
);

// 更新规格类型
router.put(
  API_URLS.SPEC_TYPE_UPDATE,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AuthMiddleware.validate(updateSpecTypeSchema),
  AdminProductController.updateSpecType
);
// 删除规格类型
router.delete(
  API_URLS.SPEC_TYPE_DELETE,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  requireAdmin,
  AdminProductController.deleteSpecType
);

// 错误处理
router.use(errorHandler);

export default router;
